package com.foodplus.www.api;

/**
 * Created by WangZhiChao on 2016/10/31.
 */

import com.foodplus.www.api.services.BasketService;
import com.foodplus.www.api.services.GoodService;
import com.foodplus.www.api.services.HomeService;
import com.foodplus.www.api.services.LoginService;
import com.foodplus.www.api.services.ShopService;
import com.foodplus.www.api.services.SpecialService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {

    public static final String BASE_URL = "http://interface.yueshichina.com/";

    private static volatile RetrofitManager instance = null;
    private static Retrofit retrofit;
    OkHttpClient client;
    private static final int DEFAULT_READ_TIMEOUT = 15 * 1000;
    private static final int DEFAULT_CONNECT_TIMEOUT = 15 * 1000;
    private static final int DEFAULT_WRITE_TIMEOUT = 15 * 1000;


    private RetrofitManager() {
        initOkhttpClient();
        initRetrofit();
    }

    public static RetrofitManager getInstance() {
        if (instance == null) {
            synchronized (RetrofitManager.class) {
                if (instance == null) {
                    instance = new RetrofitManager();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化service
     */
    public void initRetrofit() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(BASE_URL)
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create(getGson()))
                    .build();
        }
    }


    private void initOkhttpClient() {
        if (client == null) {
            client = new OkHttpClient.Builder()
                    .readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.MILLISECONDS)
                    .connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                    .writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                    .build();
        }
    }


    public Gson getGson() {
        return new GsonBuilder()
                .serializeNulls()
                .create();
    }


    public ShopService getShopService() {
        return retrofit.create(ShopService.class);
    }

    public HomeService getHomeService() {
        return retrofit.create(HomeService.class);
    }

    public SpecialService getSpecialService() {
        return retrofit.create(SpecialService.class);
    }

    public LoginService getLoginService() {
        return retrofit.create(LoginService.class);
    }

    public GoodService getGoodService() {
        return retrofit.create(GoodService.class);
    }

    public BasketService getBasketService() {
        return retrofit.create(BasketService.class);
    }

}
